package com.java.service;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PolicyConditions;

/**
 * Created by Administrator on 2016/7/6.
 */
@Service
public class OssServiceImpl implements OssService {

    @Autowired
    OSSClient ossClient;

    @Value("${sys.aliyun.oss.accessKeyId}")
    String accessKeyId;
    @Value("${sys.aliyun.oss.accessKeySecret}")
    String accessKeySecret;
    @Value("${sys.aliyun.oss.endpoint}")
    String endpoint;
    @Value("${sys.aliyun.oss.bucket}")
    String bucket;

    @Override
    public String putObject(String bucketName, String firstKey, InputStream is) {

        ossClient.putObject(bucketName, firstKey, is);
        String imagePath = String.format("http://%s.oss-cn-qingdao.aliyuncs.com/%s", bucketName, firstKey);

        return imagePath;
    }

    @Override
    public OSSObject getObject(String bucketName, String firstKey) {
        return ossClient.getObject(bucketName, firstKey);
    }

    @Override
    public Map<String, String> getPolicy(String fileName) {

        int index=endpoint.indexOf("//");
        String hostPoint=endpoint.substring(index+2);
        String host = String.format("http://%s.%s",bucket,hostPoint);
        System.out.println("hhh"+host);
        OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);

        long expireTime = 30;
        long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
        Date expiration = new Date(expireEndTime);
        PolicyConditions policyConds = new PolicyConditions();
        policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
        policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, fileName);

        String postPolicy = client.generatePostPolicy(expiration, policyConds);
        byte[] binaryData = new byte[0];
        try {
            binaryData = postPolicy.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String encodedPolicy = BinaryUtil.toBase64String(binaryData);
        String postSignature = client.calculatePostSignature(postPolicy);

        Map<String, String> respMap = new LinkedHashMap<String, String>();
        respMap.put("accessid", accessKeyId);
        respMap.put("policy", encodedPolicy);
        respMap.put("signature", postSignature);
        respMap.put("dir", fileName);
        respMap.put("host", host);
        respMap.put("expire", String.valueOf(expireEndTime / 1000));
        return respMap;

    }
}
